package com.mtl.inmigrant.shoes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.SharedPreferences;

import com.mtl.inmigrant.shoes.entity.ActionPageHelper;
import com.mtl.inmigrant.shoes.entity.ActivityPage;
import com.mtl.inmigrant.shoes.entity.ReferentialPage;
import com.mtl.inmigrant.shoes.entity.ShoeActionsInmigrant;
import com.mtl.inmigrant.shoes.entity.TaskPage;
import com.mtl.inmigrant.shoes.entity.UtilityShoesEntityApp;
import com.mtl.inmigrant.shoes.entity.TypeActivityShoes.typeActionShoe;
import com.mtl.inmigrant.shoes.sqlite.ShoesDataSource;



public class ActionShoeActivityHelper {
	
	
	public boolean hasUserLoadTables(ShoesDataSource dataSourceP){
		
		List<ShoeActionsInmigrant> values = dataSourceP.getAllShoesActions();
		
		if(values!=null && values.size() > 0 ){
			return true;
		}
		
		return false;
	}
	
	
	public void compareTablesAndPage(ShoesDataSource dataSourceP, SharedPreferences preferences) throws IOException{
		
		HashMap<String,String> keysActions = new HashMap<String, String>();
		
		loadListePages(preferences);
		
		keysActions = dataSourceP.getCurrentKeys();
		
		//find if there is any new action type-Activity
		List<ActivityPage> listeActivityPage = ActionPageHelper.getActivitiesPage();
		
		if(listeActivityPage != null && listeActivityPage.size() > 0){
			
			compareListActivitiesFromPage(dataSourceP, listeActivityPage, keysActions );
		}
		

		//List<ShoeActionsInmigrant> myListEnfants = dataSourceP.getActionEnfants("A1", typeActionShoe.ACTIVITY.toString());
		
		
	}
	
	
	private void compareListActivitiesFromPage(ShoesDataSource dataSourceP, List<ActivityPage> listeActivityPageP, 
												HashMap<String,String> keysActionsP){
		
		
		for (int a=0; a < listeActivityPageP.size(); a++){
			
			ActivityPage activityPage = listeActivityPageP.get(a);
			String keyActivity = activityPage.getIdActivity();			
			
			if(!keysActionsP.containsKey(keyActivity))
			 {
				// create new activity and his enfants
				createAllHierarchyOnActivityAction(dataSourceP, activityPage);
				
				
			 }else{				 
				 //check for any task if already exists in BD				 
				 List<TaskPage> listeTaskPageP = activityPage.getListTasks();
				 //update le nom!!!!
				 dataSourceP.updateDescriptionActivity(new String []{keyActivity},activityPage.getNameActivity());
				 
				 if(listeTaskPageP != null && listeTaskPageP.size() > 0){
					 
						 for(int t=0; t < listeTaskPageP.size(); t++){
							 
							 TaskPage taskPage = listeTaskPageP.get(t);
							 String keyTask = taskPage.getIdTask();
							 
							 if(!keysActionsP.containsKey(keyTask))
							 {
								 createAllHierarchyBasedOnTaskAction(dataSourceP, taskPage, keyActivity); 
							 }else{
								 								 
								 //uodate le nom du task!!!
								 String keyFromObjectPageT = UtilityShoesEntityApp.getStringFromAction(taskPage.getIdTask(), "onlyId", 1);
								 dataSourceP.updateDescriptionTask(new String []{keyFromObjectPageT, keyActivity }, taskPage.getNameTask());
								 
								//check for any reference if already exist in BD
								 List<ReferentialPage> listeReferentialPage =  taskPage.getListReferentials();
								 
								 if(listeReferentialPage != null && listeReferentialPage.size() > 0){
									 
									 for(int r=0; r < listeReferentialPage.size(); r++){
										 
										 ReferentialPage refPageObject = listeReferentialPage.get(r);
										 String keyReferential = refPageObject.getIdReferential();
										 
										 if(!keysActionsP.containsKey(keyReferential)){
											 
											 	ShoeActionsInmigrant shoeActionR = referentialPageToShoeAction(refPageObject);
												
											 	dataSourceP.createShoeAction(shoeActionR, keyActivity, keyFromObjectPageT);
											 
										 }else{
											 //update le nom dela reference
											 String keyFromObjectPageR = UtilityShoesEntityApp.getStringFromAction(refPageObject.getIdReferential()
													 						, "onlyId", 2);
											 dataSourceP.updateDescriptionReferencial(new String []{keyFromObjectPageR, keyFromObjectPageT,keyActivity},
													 								refPageObject.getName());
											 
										 }										 
										 
									 }		
								 }
							 }
							 
						 }
				 	}
				
			   }
			
			 }
		
		
	}
	
	
	
	
	public void loadTablesUsignInfoPage(ShoesDataSource dataSourceP, SharedPreferences preferences) throws IOException{
		
		
		loadListePages(preferences);		
		//create in BD action type ACTIVITY
		List<ActivityPage> listeActivityPageP = ActionPageHelper.getActivitiesPage();
		
		if(listeActivityPageP!=null && listeActivityPageP.size() >0){
			for( int q=0; q < listeActivityPageP.size(); q++){
				
				ActivityPage activityPage = listeActivityPageP.get(q);		
				createAllHierarchyOnActivityAction(dataSourceP, activityPage);
			}
		}

	}
	
	
	
	private void createAllHierarchyOnActivityAction(ShoesDataSource dataSourceP, ActivityPage activityPage){
		
		ShoeActionsInmigrant shoeActionA;
		

			shoeActionA = activityPageToShoeAction (activityPage);
			dataSourceP.createShoeAction(shoeActionA, null, null); //create the activity
			
			//create in bd the task associated
			List<TaskPage> listeTaskPage = activityPage.getListTasks();
			String keyActivityFather = shoeActionA.getIdActivity();
			
			if(listeTaskPage !=null && listeTaskPage.size() > 0){
			
				for (int s=0; s < listeTaskPage.size(); s++){
					
					TaskPage taskPage = listeTaskPage.get(s);			
					createAllHierarchyBasedOnTaskAction(dataSourceP, taskPage, keyActivityFather);									
					
				}
			}		
	}
	
	private void createAllHierarchyBasedOnTaskAction(ShoesDataSource dataSourceP, TaskPage taskPageP, String keyActivityFatherP){
		ShoeActionsInmigrant shoeActionT;
		
		shoeActionT = taskPageToShoeAction (taskPageP);
		dataSourceP.createShoeAction(shoeActionT, keyActivityFatherP.trim(), null); //create the activity			
		
		
		//create in BD action type REFENTIAL
		List<ReferentialPage> listeReferentialPage = taskPageP.getListReferentials();
		
		String keyTaskFhater = shoeActionT.getIdActivity().trim();
		
		if(listeReferentialPage != null && listeReferentialPage.size() > 0){
			
			for(int t=0; t < listeReferentialPage.size(); t++){
				ReferentialPage referePage = listeReferentialPage.get(t);
				
				shoeActionT = referentialPageToShoeAction(referePage);
				dataSourceP.createShoeAction(shoeActionT,keyActivityFatherP, keyTaskFhater);							
				
			}						
		}	
		
		
	}
	
	 private void loadListePages(SharedPreferences preferences) throws IOException{
			
			List<ActivityPage> listeActivityPage = new ArrayList<ActivityPage>();
			List<TaskPage> listeTaskPage = new ArrayList<TaskPage>();
			List<ReferentialPage> listeReferentialPage = new ArrayList<ReferentialPage>();
			
			ActionPageHelper.createListActionsFromPage(preferences);
			
			// referential list
			
			listeReferentialPage = ActionPageHelper.getReferentialPages();
			
			
				 				
			// task List
			listeTaskPage = ActionPageHelper.getTaskPages();
			//update sub-action-enfant list referential	 			
			if((listeTaskPage != null && listeTaskPage.size()>0) && (listeReferentialPage != null && listeReferentialPage.size() > 0)){
				
	 			for(int y=0; y < listeTaskPage.size(); y++){
	 				
	 				TaskPage taskPage = listeTaskPage.get(y);
	 				List<ReferentialPage> listeReferentialPageEnfants;	
	 				String currentTask = taskPage.getIdTask();			 				
	 				
	 				listeReferentialPageEnfants = UtilityShoesEntityApp.getReferentialPageEnfants(listeReferentialPage, currentTask);
	 				
	 				if (listeReferentialPageEnfants.size() > 0){
	 					taskPage.setListReferentials(listeReferentialPageEnfants);
	 				}else{
	 					taskPage.setListReferentials(null);
	 				}
	 				
	 			}
			}
			
			
			
		
		//Activites page	
			listeActivityPage = ActionPageHelper.getActivitiesPage();
			
			
			if((listeActivityPage != null && listeActivityPage.size() > 0) && (listeTaskPage != null && listeTaskPage.size() > 0)){
				
				
				for(int k=0; k < listeActivityPage.size(); k++){
					ActivityPage activityPage = listeActivityPage.get(k);
					List<TaskPage> listTaskEnfants;
					String currentActivity = activityPage.getIdActivity();
					
					listTaskEnfants = UtilityShoesEntityApp.getTaskPageEnfants(listeTaskPage, currentActivity);
					
					if(listTaskEnfants.size() > 0){
	 					activityPage.setListTasks(listTaskEnfants);
	 				}else{
	 					activityPage.setListTasks(null);
	 				}
					
				}	 				
				
			}		
			
			ActionPageHelper.setReferentialPages(listeReferentialPage);
			ActionPageHelper.setTaskPages(listeTaskPage);
			ActionPageHelper.setActivitiesPage(listeActivityPage);
			
	 }
	 
	 
	 
	 private ShoeActionsInmigrant activityPageToShoeAction(ActivityPage activityPageP){
		 
		 ShoeActionsInmigrant shoeAction = new ShoeActionsInmigrant();
		 shoeAction.setIdActivity(activityPageP.getIdActivity().trim());
		 shoeAction.setNameAction(activityPageP.getNameActivity());		  
		 shoeAction.setTypeAction(typeActionShoe.ACTIVITY.toString());
		 shoeAction.setNameAction(activityPageP.getNameActivity());
		 
		 return shoeAction;		 
	 }
	 
	 
	 
  private ShoeActionsInmigrant taskPageToShoeAction(TaskPage taskPageP){
		 
		 ShoeActionsInmigrant shoeAction = new ShoeActionsInmigrant();		 
		 shoeAction.setIdActivity(UtilityShoesEntityApp.getStringFromAction(taskPageP.getIdTask(),"onlyId",1).trim());
		 shoeAction.setNameAction(taskPageP.getNameTask());		  
		 shoeAction.setTypeAction(typeActionShoe.TASK.toString());
		 shoeAction.setNameAction(taskPageP.getNameTask());
		 
		 return shoeAction;		 
	 }
  
  
  private ShoeActionsInmigrant referentialPageToShoeAction(ReferentialPage referentialPageP){
		 
		 ShoeActionsInmigrant shoeAction = new ShoeActionsInmigrant();
		 shoeAction.setIdActivity(UtilityShoesEntityApp.getStringFromAction(referentialPageP.getIdReferential(),"onlyId",2).trim());
		 shoeAction.setNameAction(referentialPageP.getName());		  
		 shoeAction.setTypeAction(typeActionShoe.REFERENTIAL.toString());
		 shoeAction.setNameAction(referentialPageP.getName());
		 
		 return shoeAction;		 
	 }
	
	
	

}
